package com.application.Tree;

import java.util.ArrayList;

import com.application.Activity.MESApplication;
import com.application.Activity.R;
import com.application.Network.Protocol;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;


public class TreeMenu {
	final public static int ADD=1;
	final public static int REMOVE=2;
	final public static int EDIT=3;
	final public static int NOTE=4;
	final public static int DETAILS=5;
	final public static int ARRANGE=6;
	final public static int ZOOM_IN=7;
	final public static int ZOOM_OUT=8;
	final public static int MOVE=12;
	final public static int Avarta1=9;
	public static float ITEM_HEIGHT=40;
	final public static int space=10;
	final public static int space2=11;
	public static float SPACE=10;
	float width=120;
	float heigh=ITEM_HEIGHT;
	int ScreenW,ScreenH;
	 float x=10;
	 float y=10;
	private boolean showed;
	private boolean showedAvarta;
	ArrayList<MenuItem> items;
	public void TreeMenu(){
		
//		this.showed=true;
	}
	public void onDraw(Canvas canvas){		
		if(!showed)
			return;
		
		for(MenuItem item:items){
			item.onDraw(canvas);	
			
		}		
	}
	public void setPosition(float x, float y){
		this.x = x;
		this.y = y;
		try {
			resizeItem();
		} catch (Exception e) {
			Log.e(Protocol.TAG, "TreeMenu resizeItem");
		}
	}
	public int getCollisionItemId(float x, float y){
		int rs=-1;
		if(!this.showed)
			return rs;
		for(MenuItem item:items){
			if(item.isConllison(x, y)){
				return item.getId();
			}
		}
		return rs;
	}
	public void createEditMenu(){
		WindowManager windowManager = (WindowManager)MESApplication.getAppContext().getSystemService("window");
		ScreenW = windowManager.getDefaultDisplay().getWidth();
		ScreenH = windowManager.getDefaultDisplay().getHeight();
		Bitmap add, remove,edit,note,details,arrange,bmAvarta1,space1,space2;
		
		if(ScreenW>400)
			ITEM_HEIGHT=ScreenW/25;
		else
			ITEM_HEIGHT=ScreenW/12;
		Resources resources=MESApplication.getAppContext().getResources();
		
		add=BitmapFactory.decodeResource(resources, R.drawable.add);
		remove=BitmapFactory.decodeResource(resources, R.drawable.remove);
		edit=BitmapFactory.decodeResource(resources, R.drawable.update);
		note=BitmapFactory.decodeResource(resources, R.drawable.notes);
		details=BitmapFactory.decodeResource(resources, R.drawable.details);
		arrange=BitmapFactory.decodeResource(resources, R.drawable.arrange);
		
		float delta= ScreenW/25;
		
		items=new ArrayList<MenuItem>();
		MenuItem mnuAdd,mnuRemove, mnuEdit, mnuNote, mnuDetails, mnuArrange,mnuAvarta1,mnuspace1,mnuspace2;
		mnuAdd=new MenuItem(ADD, x, y, add);
		mnuRemove=new MenuItem(REMOVE, x+delta, y, remove);
		mnuEdit=new MenuItem(EDIT, x+2*delta, y, edit);
		mnuNote=new MenuItem(NOTE, x+3*delta, y, note);
		mnuDetails=new MenuItem(DETAILS, x+4*delta, y, details);
		mnuArrange=new MenuItem(ARRANGE, x+5*delta,y, arrange);
		
		items.add(mnuAdd);
		items.add(mnuRemove);
		items.add(mnuEdit);
		items.add(mnuNote);
		items.add(mnuDetails);		
		items.add(mnuArrange);
				
		resizeItem();
		
	}
	public void createZoomMenu(){
		Resources resources=MESApplication.getAppContext().getResources();
		Bitmap bmZoomIn, bmZoomOut;
		MenuItem mnuZoomIn, mnuZoomOut;
		
		bmZoomIn=BitmapFactory.decodeResource(resources, R.drawable.zoom_in);
		bmZoomOut=BitmapFactory.decodeResource(resources, R.drawable.zoom_out);
		
		
		mnuZoomIn=new MenuItem(ZOOM_IN, x, y, bmZoomIn);
		mnuZoomOut=new MenuItem(ZOOM_OUT, x, y, bmZoomOut);
		items=new ArrayList<MenuItem>();
		items.add(mnuZoomIn);
		items.add(mnuZoomOut);
		resizeItem();		
	}
	
	

	private void resizeItem() {
		this.width=(ITEM_HEIGHT+SPACE)*this.items.size();
		for (int i = 0; i < this.items.size(); i++) {
			this.items.get(i).setX(this.x + (ITEM_HEIGHT + SPACE) * i);
			this.items.get(i).setHeight(ITEM_HEIGHT);
			this.items.get(i).setWidth(ITEM_HEIGHT);
			this.items.get(i).setY(this.y);
		}
	}
	public boolean isShowed() {
		return showed;
	}
	public void setShowed(boolean showed) {
		this.showed = showed;
	}
	public float getX() {
		return x;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
	
}
